const errorType = require('../constant/errorType')
const errorHandle = (error, ctx) => {
  let status, body
  switch (error.message) {
    case errorType.USER_OR_PASSWORD_CAN_NOT_BE_EMPTY:
      status = 400
      body = {
        status: 400,
        msg: '用户名或者密码不能为空~'
      }
      break;
    case errorType.USER_ALREADY_EXISTS:
      status = 409
      body = {
        status: 409,
        msg: '用户名已经存在~'
      }
      break;
    case errorType.USER_NOT_EXISTS:
      status = 400
      body = {
        status: 400,
        msg: '用户不存在，请先注册~'
      }
      break;
    case errorType.PASSWORD_IS_INCORRECT:
      status = 400
      body = {
        status: 400,
        msg: '密码不正确~'
      }
      break;
    case errorType.UNAUTHORIZATION:
      status = 401
      body = {
        status: 401,
        msg: '无效的token~'
      }
      break;
    case errorType.ADDRESS_IS_EMPTY:
      status = 400
      body = {
        status: 401,
        msg: '未添加地址~'
      }
      break;
    default:
      status = 404
      body = {
        status: 404,
        msg: 'NOT FOUND '
      }
      break;
  }
  ctx.status = status
  ctx.body = body
}

module.exports = errorHandle